/*
 * @lc app=leetcode.cn id=997 lang=javascript
 *
 * [997] 找到小镇的法官
 */

// @lc code=start
/**
 * @param {number} N
 * @param {number[][]} trust
 * @return {number}
 */
var findJudge = function(N, trust) {
    let set=new Set()
    let  arr=[]
    for(let i=1;i<=N;i++){
        set.add(i)
    }
    for(let i=0;i<trust.length;i++){
        set.delete(trust[i][0])
        if(!arr[trust[i][0]]){
            arr[trust[i][0]]=[trust[i][1]]
        }else{
            arr[trust[i][0]].push(trust[i][1])
        }
    }
    if(set.size!=1)return -1
    let num=[...set][0]
    for(let i=1;i<=N;i++){
        if(i!=num&&!arr[i].includes(num))return -1
    }
    return num
};
// @lc code=end

findJudge(2,[[1,2]])